package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.common.KeyValueBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManagePosiBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageRespBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManagePosiRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManagePosiActivity extends BaseActivity {

    private TextView title,confirmBtn,inventoryAddressCode
            ,inventoryAddressCode2,inventoryAddressDescription,inventoryAddressDescription2 ;
     private EditText sendPosition,receivePosition
            ;

    private int scanCount = 0;

    private PercentRelativeLayout secondRow,thirdRow,secondBlock;
    private View line1;

    private List<StorageManagePosiBean.DataBean.InventoryAddressListBean> inventoryAddressList;
    private List<KeyValueBean.ListBean> ks;

    private StorageManagePosiBean.DataBean.InventoryAddressListBean inventoryAddressListBean;


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storage_manage_posi);
        initView(this);
        initData(this);
    }
    private void initView(Activity activity) {

        title = (TextView) activity.findViewById(R.id.headerTitle);
        inventoryAddressCode= (TextView) activity.findViewById(R.id.inventoryAddressCode);
        inventoryAddressCode2= (TextView) activity.findViewById(R.id.inventoryAddressCode2);
        inventoryAddressDescription= (TextView) activity.findViewById(R.id.inventoryAddressDescription);
        inventoryAddressDescription2= (TextView) activity.findViewById(R.id.inventoryAddressDescription2);
        sendPosition= (EditText) activity.findViewById(R.id.sendPosition);
        receivePosition= (EditText) activity.findViewById(R.id.receivePosition);
        confirmBtn= (TextView) activity.findViewById(R.id.confirmBtn);

        secondRow= (PercentRelativeLayout) activity.findViewById(R.id.secondRow);
        thirdRow= (PercentRelativeLayout) activity.findViewById(R.id.thirdRow);
        secondBlock= (PercentRelativeLayout) activity.findViewById(R.id.secondBlock);
        line1= (View) activity.findViewById(R.id.line1);




    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_storage_manage_posi_title);

        ActivityUtils.initBackAndFunction(activity, R.string.activity_storage_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        setmScanListener((String scanResult, int length) -> {
            //检查发出仓库是否为空
            String position = QRCodeUtils.DecryptQRCode(scanResult);
            if(TextUtils.isEmpty(inventoryAddressCode.getText())){
                Utils.middleToast(activity,"请选择发出仓库");
                return;
            }else {
                scanCount++;
                if(scanCount%2!=0){
                    //奇数次
                    sendPosition.setText(position);
                }else{
                    //偶数次
                    receivePosition.setText(position);
                }
            }
        },QRCodeConstant.TYPE_POSITION);

        StorageManagePosiRequest.request(activity, new GsonBeanListener<StorageManagePosiBean>() {
            @Override
            public void onSuccess(StorageManagePosiBean bean) {
                inventoryAddressList = bean.getData().getInventoryAddressList();
                ks = new ArrayList<KeyValueBean.ListBean>();
                for (StorageManagePosiBean.DataBean.InventoryAddressListBean b :inventoryAddressList){
                    KeyValueBean.ListBean k = new KeyValueBean.ListBean();
                    k.setCode(b.getInventoryAddressCode());
                    k.setValue(b.getInventoryAddressCode());
                    ks.add(k);
                }

            }
        });

        inventoryAddressCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getSelectDialog(activity, ks, new DialogUtils.SelectListener() {
                    @Override
                    public void onSelect(Dialog dialog, String value, String code) {
                        dialog.cancel();
                        inventoryAddressCode.setText(code);
                        inventoryAddressCode.setTextColor(ContextCompat.getColor(activity, R.color.common_font_content));
                        inventoryAddressListBean = find(code);
                        if(inventoryAddressListBean!=null){
                            secondRow.setVisibility(View.VISIBLE);
                            thirdRow.setVisibility(View.VISIBLE);
                            secondBlock.setVisibility(View.VISIBLE);
                            line1.setVisibility(View.VISIBLE);

                            inventoryAddressDescription.setText(inventoryAddressListBean.getInventoryAddressDescription());
                            inventoryAddressDescription2.setText(inventoryAddressListBean.getInventoryAddressDescription());
                            inventoryAddressCode2.setText(code);

                        }

                    }
                }).show();
            }
        });
        confirmBtn.setOnClickListener( new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {


                String sendPosition = StorageManagePosiActivity.this.sendPosition.getText().toString();
                String receivePosition = StorageManagePosiActivity.this.receivePosition.getText().toString();
                if(TextUtils.isEmpty(sendPosition)){
                    Utils.middleToast(activity,activity.getString(R.string.activity_storage_manage_sendPosition_no_empty_prompt));
                    StorageManagePosiActivity.this.sendPosition.requestFocusFromTouch();
                    return;
                }
                if(TextUtils.isEmpty(receivePosition)){
                    Utils.middleToast(activity,activity.getString(R.string.activity_storage_manage_recPosition_no_empty_prompt));
                    StorageManagePosiActivity.this.receivePosition.requestFocusFromTouch();
                    return;
                }

                inventoryAddressListBean.setSendPosition(sendPosition);

                inventoryAddressListBean.setReceivePosition(receivePosition);

                StorageManagePosiRequest.submitPosi(activity,inventoryAddressListBean, new GsonBeanListener<StorageManageRespBean>() {
                    @Override
                    public void onSuccess(StorageManageRespBean bean) {
                        if(bean.getData().getCode().equals(RequestConstant.BEAN_CODE_SUCCESS) ){
                            Intent intent = new Intent(activity, StorageManageResultActivity.class);
                            startActivity(intent);
                        }else{
                            Utils.middleToast(activity, "提交失败:"+bean.getData().getMsg());
                        }

                    }
                });
            }
        });




    }

    private StorageManagePosiBean.DataBean.InventoryAddressListBean find(String code) {
        for (StorageManagePosiBean.DataBean.InventoryAddressListBean b :inventoryAddressList){
           if (b.getInventoryAddressCode().equals(code)){
               return b;
           }
        }
        return null;
    }
}
